home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Shareware World / Comms & Internet / ImageMapper v3.0 / Goodies / Java / java / Source / HighlightedImageMap.java < prev   
Text File  |  1998-03-08  |  7KB  |  283 lines

  1. /**
  2.  *
  3.  *    HighlightedImageMap
  4.  *
  5.  *    This applet was originally developed by Rickard Oberg (rickard@lysator.liu.se)
  6.  *    
  7.  *    Modifications were made by Stuart Snaddon (snaddosg@dcs.gla.ac.uk) to make the applet:
  8.  *        • More stable (crashes were reported with the original applet)
  9.  *        • Capable of playing sounds if required
  10.  *        • Show the 'ALT' text in the browser status bar (if both exist!)
  11.  *
  12.  *    These modifications are intended to make the applet more useful with the macintosh
  13.  *    application ImageMapper v3.0 (shareware $15), but with the original spirit of Rickard's applet,
  14.  *    this code has been placed in the public domain, feel free to modify and distribute, but please
  15.  *    keep this notice intact.
  16.  *
  17.  */
  18.  
  19. import java.applet.*;
  20. import java.awt.*;
  21. import java.awt.image.*;
  22. import java.io.*;
  23. import java.net.*;
  24. import java.util.*;
  25.  
  26. public class HighlightedImageMap extends Applet
  27. {
  28.  
  29.     Image                 normalimg;
  30.     Image                 highlightedimg;
  31.  
  32.     public boolean        nloaded=false;
  33.     public boolean        hloaded=false;
  34.  
  35.     boolean             debug=false;
  36.  
  37.     public void start()
  38.     {
  39.     
  40.         // Check if the images are already loaded (Cache fix)
  41.         if ((checkImage(normalimg, this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
  42.         {
  43.             nloaded = true;
  44.         }
  45.         
  46.         if ((checkImage(highlightedimg, this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
  47.         {
  48.             hloaded = true;
  49.         }
  50.         
  51.         // Paint it!
  52.         repaint();
  53.         
  54.     }
  55.     
  56.     public void init()
  57.     {
  58.         //Applet name
  59.         System.out.println("\nThis applet is a modified, enhanced version of:");
  60.         System.out.println("HighlightedImageMap v1.11, by Rickard ÷berg(rickard@lysator.liu.se) 1996");
  61.         System.out.println("\nPart of ImageMapper v3.0");
  62.         System.out.println("(c)1997 Stuart Snaddon(snaddosg@kagi.com)\n");
  63.         
  64.         //Show "Loading"
  65.         repaint();
  66.  
  67.         //Show debug(X/Y) info?
  68.         debug=(getParameter("debug")==null)?false:true;
  69.          
  70.         //Target frame name. If null then same frame is target
  71.         //Either one target for every link, or one for each link
  72.  
  73.         String target=getParameter("target");
  74.         if (target==null)
  75.             target="";
  76.  
  77.         boolean individualtargets=false; //If true, each link has it's own target frame/window
  78.         StringBufferInputStream tsbis=new StringBufferInputStream(target);
  79.         StreamTokenizer         tst=new StreamTokenizer(tsbis);
  80.         
  81.         if (target.indexOf(',')!=-1)
  82.             individualtargets=true; //Individual link targets
  83.         
  84.         //Get images
  85.         URL normalurl, highlightedurl;
  86.         try
  87.         {
  88.             normalurl=new URL(getDocumentBase(),getParameter("normal"));
  89.             highlightedurl=new URL(getDocumentBase(),getParameter("highlight"));
  90.         }
  91.         catch(Exception e)
  92.         {
  93.             System.out.println("Error: 'normal' or 'highlight' not valid");
  94.             return;
  95.         }
  96.  
  97.         normalimg = getImage(normalurl);
  98.         highlightedimg = getImage(highlightedurl);
  99.     
  100.         prepareImage(normalimg, this);
  101.         prepareImage(highlightedimg, this);
  102.             
  103.         setLayout(null);
  104.             
  105.         repaint();
  106.         resize(normalimg.getWidth(this), normalimg.getHeight(this));
  107.  
  108.         /*
  109.          *
  110.          * Parse links
  111.          *
  112.          */
  113.  
  114.         String links=getParameter("links");
  115.  
  116.         //No links, no imagemap
  117.         if (links==null)
  118.         {
  119.             System.out.println("Error: no links specified");
  120.             return;
  121.         }
  122.  
  123.         StringBufferInputStream sbis=new StringBufferInputStream(links);
  124.         StreamTokenizer         st=new StreamTokenizer(sbis);
  125.         st.quoteChar(39);
  126.         
  127.         //Syntax {ulx,uly,lrx,lry,"URL"}
  128.         int x,y,w,h;
  129.         URL url = null, audioURL = null; //Link URLs
  130.         String alt;
  131.         boolean done=false; //Done parsing links?
  132.  
  133.         try
  134.             do
  135.             {
  136.                 //Default alternative text
  137.                 alt="";
  138.  
  139.                 if (st.nextToken()!='{') throw new Exception("{ expected");
  140.         
  141.                 //Upper left x
  142.                 if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
  143.                 x=(int)st.nval;
  144.         
  145.                // ','
  146.                if (st.nextToken()!=',') throw new Exception("',' expected");
  147.         
  148.                //Upper left y
  149.                if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
  150.                y=(int)st.nval;
  151.  
  152.                // ','
  153.                if (st.nextToken()!=',') throw new Exception("',' expected");
  154.  
  155.                //Lower right x
  156.                if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
  157.                w=(int)st.nval-x;
  158.  
  159.                // ','
  160.                if (st.nextToken()!=',') throw new Exception("',' expected");
  161.         
  162.                //Lower right y
  163.                if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
  164.                h=(int)st.nval-y;
  165.         
  166.                // ','
  167.                if (st.nextToken()!=',') throw new Exception("',' expected");
  168.         
  169.                //URL
  170.                if (st.nextToken()!=39) throw new Exception("quoted url expected(use ' for quotation)");
  171.                url=new URL(getDocumentBase(),st.sval);
  172.  
  173.                //Optional alternative text
  174.                if (st.nextToken()==',')
  175.                {
  176.                    if (st.nextToken()==39)
  177.                        alt=st.sval;
  178.                    else
  179.                        throw new Exception("alternative linktext expected");
  180.                }
  181.                else
  182.                    st.pushBack();
  183.         
  184.                //Optional AudioClip URL
  185.                if (st.nextToken()==',')
  186.                {
  187.                    if (st.nextToken()==39)
  188.                        audioURL=new URL(getDocumentBase(),st.sval);
  189.                    else
  190.                           throw new Exception("quoted url expected(use ' for quotation)");
  191.                }
  192.                else
  193.                    st.pushBack();
  194.  
  195.                if (st.nextToken()!='}')
  196.                       throw new Exception("} expected");
  197.  
  198.                //Get target name
  199.                String linktarget; //Target for this particular link
  200.                if (individualtargets)
  201.                {
  202.                    tst.nextToken();
  203.                    linktarget=tst.sval;
  204.                    tst.nextToken();
  205.                }
  206.                else
  207.                    linktarget=target; //Use global target
  208.         
  209.                //Create images for link
  210.                ImageFilter cropfilter = new CropImageFilter(x,y,w,h);
  211.                Image nimg = createImage(new FilteredImageSource(normalimg.getSource(),
  212.                                         cropfilter));
  213.  
  214.                Image hlimg = createImage(new FilteredImageSource(highlightedimg.getSource(),
  215.                                          cropfilter));
  216.  
  217.                //Add link
  218.                add(new HighlightableLink(x, y, w, h, nimg, hlimg, url, audioURL, alt, linktarget, this));
  219.  
  220.                //Check if end of links
  221.                if (st.nextToken()==StreamTokenizer.TT_EOF)
  222.                    done=true;
  223.  
  224.                st.pushBack();
  225.  
  226.             } while(!done);
  227.         catch(Exception e)
  228.         {
  229.             System.out.println("Error in links parameter:");
  230.             System.out.println(e);
  231.         }
  232.     }
  233.  
  234.     public void paint(Graphics g)
  235.     {
  236.         if (nloaded && hloaded)
  237.         {
  238.             g.drawImage(normalimg,0,0,this);
  239.         }
  240.         else
  241.         {
  242.             g.drawString("Loading",0,10);
  243.         }
  244.         
  245.         paintComponents(g);
  246.     }
  247.  
  248.     public void update(Graphics g)
  249.     {
  250.         paint(g);
  251.     }
  252.         
  253.     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
  254.     {
  255.         if (img==normalimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
  256.         {
  257.             nloaded = true;
  258.         }
  259.         
  260.         if (img==highlightedimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
  261.         {
  262.             hloaded = true;
  263.         }
  264.         
  265.         if (nloaded && hloaded)
  266.             repaint();
  267.             
  268.         return !(hloaded && nloaded);
  269.     }
  270.         
  271.     public boolean mouseMove(Event evt, int x, int y)
  272.     {
  273.         //Show debug(X/Y) info
  274.         if (debug)
  275.         {    
  276.             showStatus("X: "+x+" Y: "+y);
  277.             return true;
  278.         }
  279.  
  280.         return false;
  281.     }
  282.  
  283. }